package kassiopeia.agent.plugin;

import java.io.File;
import java.util.List;
import java.util.Map;
import kassiopeia.agent.alerting.PluginAlerting;
import kassiopeia.agent.infrastructure.AliveAgentsHolder;
import kassiopeia.agent.infrastructure.EventRouter;
import kassiopeia.plugin.Context;
import kassiopeia.plugin.Event;
import kassiopeia.plugin.Logger;

public class ContextImpl implements Context {

    private String agentId;
    private String instanceId;
    private ClassLoader classLoader;
    private File pluginTemporaryDirectory;
    private AliveAgentsHolder aliveAgentsHolder;
    private Map<String, String> parameters;
    private List<String> outputsIds;
    private PluginAlerting alerting;
    private EventRouter eventRouter;

    @Override
    public String getThisAgentId() {
        return agentId;
    }

    @Override
    public List<String> getOtherAgentsIds() {
        return aliveAgentsHolder.getAliveAgentsIds();
    }

    @Override
    public File getTemporaryDirectory() {
        return pluginTemporaryDirectory;
    }

    @Override
    public void send(Event event, String pluginOutputId) {
        validateOutputId(pluginOutputId);
        eventRouter.routeEvent(event, instanceId, pluginOutputId);
    }

    @Override
    public void sendLocally(Event event, String outputId) {
        eventRouter.routeEventLocally(event, instanceId, outputId);
    }

    @Override
    public String getPluginInstanceId() {
        return instanceId;
    }

    @Override
    public void alert(String message) {
        alerting.alert(message);
    }

    @Override
    public Logger getLogger() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getParameter(String outputId) {
        return parameters.get(outputId);
    }

    @Override
    public ClassLoader getClassLoader() {
        return classLoader;
    }

    public void putParameter(String parameterName, String value) {
        if(!parameters.containsKey(parameterName)) {
            throw new IllegalArgumentException("Plugin " + instanceId + " doesn't have parameter " + parameterName);
        }

        parameters.put(parameterName, value);
    }

    public void setParameters(Map<String, String> parameters) {
        this.parameters = parameters;
    }

    public void setClassLoader(ClassLoader classLodaer) {
        this.classLoader = classLodaer;
    }

    public void setOutputsIds(List<String> outputsIds) {
        this.outputsIds = outputsIds;
    }

    public void setAgentId(String agentId) {
        this.agentId = agentId;
    }

    public void setInstanceId(String instanceId) {
        this.instanceId = instanceId;
    }

    public void setPluginTemporaryDirectory(File pluginTemporaryDirectory) {
        this.pluginTemporaryDirectory = pluginTemporaryDirectory;
    }

    public void setAlerting(PluginAlerting alerting) {
        this.alerting = alerting;
    }

    public void setAliveAgentsHolder(AliveAgentsHolder aliveAgentsHolder) {
        this.aliveAgentsHolder = aliveAgentsHolder;
    }

    public EventRouter getEventRouter() {
        return eventRouter;
    }

    public void setEventRouter(EventRouter eventRouter) {
        this.eventRouter = eventRouter;
    }

    private void validateOutputId(String outputId) {
        if(!outputsIds.contains(outputId)) {
            throw new IllegalArgumentException("Plugin " + instanceId + " doesn't have output " + outputId);
        }
    }
}
